<template>
    <div class="shopCarContainer">
        <div class="goodsList">
            <div class="goodsItem" v-for="item in shopCarList" :key="item.id">
                <div class="mui-card">
                    <div class="mui-card-content">
                        <!---->
                        <!-- 获取 是否 选中 -->
                        <mt-switch v-model="$store.getters.getSelectedAll[item.id]"
                                   @change="$store.commit('handleSwitchChange', item.id)"></mt-switch>
                        <img :src="item.thumb_path">
                        <div class="info">
                            <h3>{{ item.title }}</h3>
                            <p>
                                <span class="price">￥{{ item.sell_price }}</span>
                                <num-box :id="item.id" :curvalue="getCount(item.id)"
                                         @getCount="updateCount"></num-box>
                                <a href="javascript:;" @click="removeItem(item.id)">删除</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="goodsMoney">
            <div class="goodsItem">
                <div class="mui-card">
                    <div class="mui-card-content">
                        <div class="left">
                            <p>总结 （不含运费）</p>
                            <p>已勾选商品 <span>{{ $store.getters.getAllCount }}</span>件
                                总价 <span>￥{{ $store.getters.getAllMoney }}</span></p>
                        </div>
                        <div class="right">
                            <mt-button type="danger">去结算</mt-button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    // 引入 shopCarNumBox 组件
    import numBox from '../subComponents/shopCarButtonComponent.vue'
    export default {
        data() {
            return {
                // 商品 列表信息
                shopCarList: []
            }
        },
        created() {
            // 开始 请求获得商品列表数据
            this.getShopCarList()
        },
        methods: {
          // 获取 购物车页面数据
          getShopCarList() {
              // 商品列表的 ID
              var ids = []
              JSON.parse(window.localStorage.getItem('car')).forEach(item => {
                  ids.push(item.id)
              })
              console.log(ids.join(','))
              this.$http.get(`api/goods/getshopcarlist/` + ids.join(','))
                  .then((data) => {
                      // 判断获取页面是否成功
                      var data = data.body
                      if (data.status !== 0) {
                          return this.$store.commit('ToastInfo', { msg: '数据获取失败' })
                      }
                      // success
                      this.shopCarList = data.message
                      console.log(this.shopCarList, '商品列表')
                  }, () => {
                      return this.$store.commit('ToastInfo', { msg: 'Server error 500', pos: 'middle' })
                  })
          },
          // 拿到子组件传过来的值 进行 处理
          updateCount(info) {
              // 以防万一 修改一下 value id  为 int
              var count = parseInt(info.value)
              var id = parseInt(info.id)
              console.log('拿到了自组件的值', count)
              console.log('拿到了自组件的值', info.id)
              // 发送一个 { id: xx, count: xxx }
              this.$store.commit('updateOrder', { id, count })
          },
          // 根据 id 获取 count
          getCount(id) {
            var count = 0
            var id = parseInt(id)
            var Data = JSON.parse(window.localStorage.getItem('car'))
            // 根据 id 去 orderList 查找  赋值 count
              Data.some(item => {
                if (item.id === id) {
                    count = item.count
                    return true
                }
            })
            return count
          },
          // 删除功能
          removeItem(id) {
              this.$store.commit('removeById', id)
              var index = this.shopCarList.findIndex(item => item.id === id)
              this.shopCarList.splice(index, 1)
          },
          // 改变了
          changed() {
              console.log('改变了')
          }
        },
        components: {
            numBox
        }
    }
</script>

<style scoped lang="less">
    .shopCarContainer {
        background-color: #eee;
        overflow: hidden;
        // 商品列表区域
        .goodsList {
            .mui-card-content {
                padding: 15px;
                padding-right: 10px;
                display: flex;
                align-items: center;
                img {
                    width: 60px;
                    height: 60px;
                    margin: 0 3px;
                }
                .info {
                    padding: 0 4px;
                    h3 {
                        font-size: 13px;
                    }
                    .price {
                        color: red;
                        font-size: 15px;
                        font-weight: bold;
                    }
                }
            }
        }
        // 结算区域
        .goodsMoney {
            .mui-card-content {
                padding: 15px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                span {
                    color: red;
                    font-size: 16px;
                    font-weight: bold;
                }
            }
        }
    }
</style>